﻿using System;
using System.IO;
using System.Text;

namespace Program07
{
    class Program
    {
        static void Main(string[] args)
        {
            ////使用FileStream来写入文件
            //using (FileStream fsWrite = new FileStream ("new.txt",FileMode.OpenOrCreate,FileAccess.Write))
            //{
            //    var str = "覆盖所有字符";
            //    //把字符串转换为二进制
            //    byte[] buffer = Encoding.UTF8.GetBytes(str);
            //    //读取当前文件的有效字节数
            //    fsWrite.Write(buffer, 0, buffer.Length);
            //}
            //Console.WriteLine("写入文件成功!");

            //读取文件
            FileStream fsRead = new FileStream(@"E:\1Rice\CsharpAdvanced\Program07\bin\Debug\netcoreapp3.0\new.txt", FileMode.OpenOrCreate, FileAccess.Read);
            //读取文件不能超过5M（定义范围）
            byte[] buffer = new byte[1024 * 1024 * 5];
            //读取当前文件的有效字节数
            int r = fsRead.Read(buffer, 0, buffer.Length);
            //将读出的二进制字节流转换成Utf8格式的字符串
            string str = Encoding.UTF8.GetString(buffer, 0, r);
            //关闭文件流
            fsRead.Close();
            //释放存储空间、线程
            fsRead.Dispose();
            Console.WriteLine(str);
        }
    }
}
